package com.lazy.lib.util;


public class Des {
	private static long M1  = 851027125;
	private static long IA1 = 410213143;
	private static long IC1 = 219143235;
	
//	public static byte[] encrypt(long key, byte[] buff, int size) {
//		int idx=0;
//		if(key==0) {
//			key = 1;
//		}
//		
//		while(idx < size) {
//			key = IA1 * (key % M1) + IC1;
//			System.out.println("key " + key);
//		    long abc = (key >>> 20) & 0xFF;
//		    long nabc= abc >= 0 ? abc : 256 + abc;
//		    int n=idx++;
//		    long b= buff[n] ^= nabc; 
//		  }
//		  return (buff);
//	}
	
	public static byte[] encrypt(long key, byte[] buff, int size) {
		int idx=0;
		if(key==0) {
			key = 1;
		}
		
		while(idx < size) {
			key = IA1 * (key % M1) + IC1;
			key = (long)(key & 0xFFFFFFFFl);
			// System.out.println("  key " + Long.toHexString(key));
			buff[idx++] ^= (byte)((key >>> 20) & 0xFF);
		}
		return (buff);
	}
	
	public static void main(String arg[]) {
		byte[] testData = new byte[] {
				0x7e,(byte) 0xc0, (byte) 0xe9, (byte) 0xe9, 0x76
		};
		
		// long key =  0x2ded897b;
		long key =  1255;
//		System.out.println("===key " + Long.toHexString(0x0ffffffffl));
//		System.out.println("===key " + Long.toHexString(key));
		
		encrypt(key, testData, 5);
		for (int i = 0; i < testData.length; i++) {
			System.out.print(String.format(" 0x%02x", testData[i]));
		}
		System.out.println();
		
		key =  1255;
		encrypt(key, testData, 5);
		for (int i = 0; i < testData.length; i++) {
			System.out.print(String.format(" 0x%02x", testData[i]));
		}
		System.out.println();
		System.out.println("" + Utils.bytesToHexString(testData, 0, 5));
	}
}
